# This PowerShell script was released under the Ms-PL license
# http://www.opensource.org/licenses/ms-pl.html
# This script was originally intended for use with Microsoft Dynamics AX 2009
# and maintained and distributed as a project on CodePlex
# http://dynamicsaxbuild.codeplex.com

param(
	[string] $AXCOMPILEXML = $(throw "Ax Compile XML File Path is Required"),
	[string] $OUTPUTSEVERITY = $(throw "Compiler Severity number(s) are Required")
	)

$ns = @{ Table = "urn:www.microsoft.com/Formats/Table" }

$SEVERITIES = $OUTPUTSEVERITY.Split(",")

$severityFilter = ""

foreach($severity in $SEVERITIES)
{
	if ($severityFilter -ne "")
	{
		$severityFilter = $severityFilter + " or "
	}
	$severityFilter = $severityFilter + "Table:Field[@name='SysCompilerSeverity'] = '" + $severity + "'"
}

$xpath = $xpath = "//Table:Record[" + $severityFilter + "]"


$nodes = (select-xml -path $AXCOMPILEXML -namespace $ns -xpath $xpath)
if ($nodes -ne $null)
{
	Foreach($node in $nodes)
	{
		$value = select-xml -Xml ($node.Node) -namespace $ns -xpath "Table:Field[@name='TreeNodePath']"
		$message = ($value.Node."#text")
		$value = select-xml -Xml ($node.Node) -namespace $ns -xpath "Table:Field[@name='Line']"
		$message = $message + ", line " + ($value.Node."#text")
		$value = select-xml -Xml ($node.Node) -namespace $ns -xpath "Table:Field[@name='Column']"
		$message = $message + ", column " + ($value.Node."#text")
		$value = select-xml -Xml ($node.Node) -namespace $ns -xpath "Table:Field[@name='SysCompileErrorMessage']"
		$message = $message + ": " + ($value.Node."#text")
		
		Write-Host $message
	}
}
